package com.minelx.woods.pc.service.mapper;

import com.alibaba.fastjson.JSONObject;
import com.minelx.httpclient.core.Requests;
import com.minelx.httpclient.core.request.IResponse;
import com.minelx.sqlstructures.common.Files;
import com.minelx.statementsplus.common.Templates;
import com.minelx.woods.pc.form.values.VariantValues;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.toJSONString;

public class ReviewRequestMapper {
	public IResponse requestPostReview(String token,
	                                   String name,
	                                   List<String> reviewers,
	                                   String sourceType,
	                                   JSONObject sourceVariables,
	                                   String[] collections,
	                                   VariantValues[] variantCollections) {
		Map<String, String> parameters = new HashMap<>();
		parameters.put("token", token);
		parameters.put("name", name);
		parameters.put("reviewers", toJSONString(reviewers));
		parameters.put("sourceType", sourceType);
		// input variables
		parameters.put("sourceVariables", sourceVariables.toJSONString());
		parameters.put("collections", toJSONString(collections));
		parameters.put("variantCollections", toJSONString(variantCollections));
		return Requests.parse(new Templates(Files.toString("/requests/post_review.request")).render(parameters)).fetch();
	}

	public IResponse requestGetReviews(String token, int offset, int length) {
		Map<String, String> parameters = new HashMap<>();
		parameters.put("token", token);
		parameters.put("offset", String.valueOf(offset));
		parameters.put("length", String.valueOf(length));
		return Requests.parse(new Templates(Files.toString("/requests/get_reviews.request")).render(parameters)).fetch();
	}

	public IResponse requestApprove(String token, String reviewId) {
		Map<String, String> parameters = new HashMap<>();
		parameters.put("token", token);
		parameters.put("reviewId", reviewId);
		return Requests.parse(new Templates(Files.toString("/requests/put_approve.request")).render(parameters)).fetch();
	}

	public IResponse requestReject(String token, String reviewId) {
		Map<String, String> parameters = new HashMap<>();
		parameters.put("token", token);
		parameters.put("reviewId", reviewId);
		return Requests.parse(new Templates(Files.toString("/requests/put_reject.request")).render(parameters)).fetch();
	}
}
